package com.jun.flower.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jun.flower.bean.PageResult;
import com.jun.flower.bean.TbFlowers;
import com.jun.flower.bean.TbUser;
import com.jun.flower.service.FlowerService;

@Controller
public class FlowerController {
	@Autowired
	protected FlowerService flowerService;
	
	@RequestMapping("/flower/show")
	public String showFlower(Model model,Integer id) {
		TbFlowers flower = flowerService.getFlowerById(id);
		model.addAttribute("flower", flower);
		return "flowers-show";
	}
	
	@RequestMapping("/flower/list")
	public String getFlowerList(Model model, @RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) {
		PageResult<TbFlowers> pageResult = flowerService.getFlowerList(pageNum,pageSize);
		model.addAttribute("flowerList",pageResult.getList() );
		model.addAttribute("page", pageResult);
		return "flowers-list";
	}
	
	@RequestMapping("/flower/check/flowernum")
	@ResponseBody
	public Map checkFlowerNum(String flowernum) {
		boolean result = flowerService.checkFlowerNum(flowernum);
		Map data = new HashMap<>();
		data.put("status", result);
		return data;
	}
	
	@RequestMapping("/flower/save")
	public String addFlower(TbFlowers flower) {
		//把鲜花添加到数据库中
		flowerService.addFlower(flower);
		//跳转到鲜花列表页面
		return "redirect:/flower/list";
	}
	
	@RequestMapping("/flower/delete")
	@ResponseBody
	public Map deleteFlower(String ids) {
		Boolean result = flowerService.deleteFlower(ids);
		Map data = new HashMap<>();
		data.put("state", result);
		return data;
	}
	
	@RequestMapping("/order/findflower")
	public String orderGetFlowerList(Model model) {
		List<TbFlowers> plist = flowerService.orderGetFlowerList();
		model.addAttribute("plist", plist);
		return "order-add";
	}
	
	@RequestMapping("/flower/update")
	public String updateFlower(Model model,Integer id) {
		TbFlowers flower = flowerService.getFlowerById(id);
		model.addAttribute("flower", flower);
		return "flowers-update";
	}
	
	@RequestMapping("/flower/updateById")
	public String updateFlowerById(TbFlowers flower) {
		 flowerService.updateFlowerById(flower);
		return "redirect:/flower/list";
	}
	

	@RequestMapping("/web/index")
	public String showWebFlower(Model model) {
		List<TbFlowers> list = flowerService.getWebFlowerList();
		model.addAttribute("flowerList", list);
		return "web-index";
	}
	
	@RequestMapping("/web/flower")
	public String showWebFlowerById(Model model,Integer id) {
		TbFlowers flower = flowerService.getFlowerById(id);
		model.addAttribute("flower", flower);
		return "web-flower";
	}
}
